﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.Locomotive.TrainRepair.Work.Fittings
{
    public partial class Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadTTID = "";//机型ID
        public string _LoadTTName = "";//机型名
        public string _LoadTLID = "";//机车BMWID
        public string _LoadTLName = "";//机车号
        public Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm_Load(object sender, EventArgs e)
        {
            this.Text = "机车号：" + _LoadTLName + "下车配件管理";
            _LoadTTName = _LoadTTName.Substring(0,_LoadTTName.IndexOf(' '));
            InitData();
        }
        private void InitData()
        {
            tl_SysDep.Nodes.Clear();
            ArrayList pList = new ArrayList();
            LeverString_TrainDepSysList p = new LeverString_TrainDepSysList();
            string strSQL = "select f_id,f_name,f_tcid,f_deep,f_level from TS_S_T_P_TrainDepSys where f_del=0 and f_deep=1 and f_name like '%"+_LoadTTName+ "%' and (f_level like '%,2,%' or f_level like '%,1,%')";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_TrainDepSysList();
                p.f_name = read["f_name"].ToString();
                p.f_id =  read["f_id"].ToString();
                p.f_deep = read["f_deep"].ToString();
                p.f_level = read["f_level"].ToString();
                p.f_tcid = read["f_tcid"].ToString();
                pList.Add(p);
            }
            read.Close();
                strSQL = "select f_id,f_name as f_name,f_num,f_deep,f_level,(select count(*) from TS_TR_TM_Loco_Fittings_Work where f_tdid=TS_S_T_P_TrainDepSys.f_id and f_bmwid=" + _LoadTLID + " and f_type=0 and f_del=0) as f_numremove,(select count(*) from TS_TR_TM_Loco_Fittings_Work where f_tdid=TS_S_T_P_TrainDepSys.f_id and f_bmwid=" + _LoadTLID + " and f_type=1 and f_del=0) as f_numinstall from TS_S_T_P_TrainDepSys where f_del=0 and f_deep=2 and f_id in (select f_tdid from TS_S_T_P_TrainDepSys_Type where f_ttid=" + _LoadTTID + " and f_del=0)  and (f_level like '%,1,%' or f_level like '%,2,%')";
                comm = new SqlCommand(strSQL, conn);
                read = comm.ExecuteReader();
                while (read.Read())
                {
                    string _PID = read["f_level"].ToString();
                    _PID = _PID.Substring(0, _PID.Length - 1);
                    _PID = _PID.Substring(_PID.LastIndexOf(',') + 1);
                    p = new LeverString_TrainDepSysList();
                    p.f_name = read["f_name"].ToString();
                    p.f_id =  read["f_id"].ToString();
                    p.f_parent =  _PID;
                    p.f_deep = read["f_deep"].ToString();
                    p.f_level = read["f_level"].ToString();
                    p.f_numcurrent = (int)read["f_num"];
                    p.f_numinstall = (int)read["f_numinstall"];
                    p.f_numremove = (int)read["f_numremove"];
                    pList.Add(p);
                }
                read.Close();
            conn.Close();
            conn.Dispose();
            tl_SysDep.DataSource = pList;
            tl_SysDep.ExpandAll();
        }

        private void tl_SysDep_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                InitGrid();
            }
            catch
            {
            }
        }
        private void InitGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,f_id,f_name as f_tdname,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_TM_Loco_Fittings_Work.f_ftid) as f_ftname,f_code,f_plname,(select f_name from TS_S_T_P_Department where f_id=TS_TR_TM_Loco_Fittings_Work.f_depid) as f_depname,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_TM_Loco_Fittings_Work.f_mapid) as f_mapname,f_type,(case f_type when 0 then '下车' else '上车' end) as f_typename,(case f_fid when -1 then ' ' else (select CONVERT(varchar(100), f_indate, 23) from TS_TR_B_Loco_Fittings_Base_List where f_id=TS_TR_TM_Loco_Fittings_Work.f_fid) end ) as f_indate from TS_TR_TM_Loco_Fittings_Work where f_bmwid=" + _LoadTLID + " and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where (f_id=" + tl_SysDep.FocusedNode["f_id"].ToString() + " or f_level like '%," + tl_SysDep.FocusedNode["f_id"].ToString() + ",%') and f_del=0) and f_del=0";
            DataTable _tb = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _tb);
            gc_Main.DataSource = _tb;
            gc_Main.Refresh();
        }
        #region//--弹出菜单--//
        private void cms_Main_Opening(object sender, CancelEventArgs e)
        {
            cms_Remove.Enabled = false;
            cms_Install.Enabled = false;
            try
            {
                if ((int)tl_SysDep.FocusedNode["f_numinstall"] < (int)tl_SysDep.FocusedNode["f_numcurrent"])
                {
                    cms_Install.Enabled = true;
                }
                else
                {
                    cms_Install.Enabled = false;
                }
                if ((int)tl_SysDep.FocusedNode["f_numremove"] < (int)tl_SysDep.FocusedNode["f_numcurrent"])
                {
                    cms_Remove.Enabled = true;
                }
                else
                {
                    cms_Remove.Enabled = false;
                }
            }
            catch
            {
            }
        }

        private void cms_Remove_Click(object sender, EventArgs e)
        {
           TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_DownTrain_DepList_ManaForm _uc =new UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_DownTrain_DepList_ManaForm();
            _uc._LoadType = "0";
            _uc._LoadBMWID = _LoadTLID;
            _uc._LoadNum = Convert.ToString((int)tl_SysDep.FocusedNode["f_numcurrent"] - (int)tl_SysDep.FocusedNode["f_numremove"]);
            _uc._LoadTDID = tl_SysDep.FocusedNode["f_id"].ToString();
            _uc._LoadTDName = tl_SysDep.FocusedNode["f_name"].ToString();
            _uc._LoadTLName = _LoadTLName;
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                InitData();
                tl_SysDep.SetFocusedNode(tl_SysDep.FindNodeByFieldValue("f_id", _uc._LoadTDID));
            }
            else
            {
            }
        }

        private void cms_Install_Click(object sender, EventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_UpTrain_DepList_ManaForm _uc = new UserCon.Locomotive.TrainRepair.Work.Fittings.uc_Locomotive_TrainRepair_Work_Fittings_UpTrain_DepList_ManaForm();
            _uc._LoadType = "1";
            _uc._LoadBMWID = _LoadTLID;
            _uc._LoadNum = Convert.ToString((int)tl_SysDep.FocusedNode["f_numcurrent"] - (int)tl_SysDep.FocusedNode["f_numinstall"]);
            _uc._LoadTDID = tl_SysDep.FocusedNode["f_id"].ToString();
            _uc._LoadTDName = tl_SysDep.FocusedNode["f_name"].ToString();
            _uc._LoadTLName = _LoadTLName;
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                InitData();
                tl_SysDep.SetFocusedNode(tl_SysDep.FindNodeByFieldValue("f_id", _uc._LoadTDID));
            }
            else
            {
            }
        }
        #endregion

        private void cms_Func_Opening(object sender, CancelEventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                cms_Func_Del.Enabled = true;
            }
            else
            {
                cms_Func_Del.Enabled = false;
            }
        }

        private void cms_Func_Del_Click(object sender, EventArgs e)
        {
            string _NowTDID = tl_SysDep.FocusedNode["f_id"].ToString();
            string strSQL = "update TS_TR_TM_Loco_Fittings_Work set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            InitData();
            tl_SysDep.SetFocusedNode(tl_SysDep.FindNodeByFieldValue("f_id", _NowTDID));
        }

        private void Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
    }
}